#/bin/sh!
SCRIPT_PATH=/net/alice22/scratch/scripts/openstack/

## ENV
echo ENV
source $SCRIPT_PATH/conffiles/ENV

servers="keystone glance nova neutron dash cinder"
controller=`hostname -i`

service_nova="openstack-nova-api
 openstack-nova-cert
 openstack-nova-consoleauth
 openstack-nova-scheduler
 openstack-nova-conductor
 openstack-nova-novncproxy
 "
service_cinder="openstack-cinder-api
 openstack-cinder-scheduler
 openstack-cinder-volume
 "
service_neutron="openstack-nova-api
 openstack-nova-scheduler
 openstack-nova-conductor
 neutron-server
 "
service_glance="openstack-glance-api
 openstack-glance-registry
 "
service02="$service_nova $service_neutron $service_glance $service_cinder "
service03="httpd memcached tgtd"



## END of ENV
echo END of ENV


## Package install
echo Package install

packages="patch mysql mysql-server MySQL-python openstack-keystone python-keystoneclient openstack-glance python-glanceclient openstack-nova-api openstack-nova-cert openstack-nova-conductor openstack-nova-console openstack-nova-novncproxy openstack-nova-scheduler python-novaclient openstack-neutron openstack-neutron-ml2 python-neutronclient memcached python-memcached mod_wsgi openstack-dashboard openstack-cinder  qpid-cpp-server "
yum -y  install $packages
## End Package install
echo End Package install

## Service setting 01
echo Service setting 01

services01="mysqld qpidd"
mv /etc/my.cnf /etc/my.cnf.save

cat > /etc/my.cnf << EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
bind-address = $controller
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

[mysqld_safe]

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF

sed s/"auth=.*"/"auth=yes"/g -i  /etc/qpidd.conf

for serv in $services01; do
        service $serv start;
        chkconfig $serv on;
done

/usr/bin/mysqladmin -u root password $Database_password --password=''
#/usr/bin/mysqladmin -u -h alice01.cloud.spbu.ru root password $Database_password

## END Service setting 01
echo END Service setting 01

##Create DBs:
echo Create DBs

for s_name in $servers; do
	S_NAME=`echo $s_name|sed y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/`
	PASS=grep ${S_NAME}_DBPASS  $SCRIPT_PATH/conffiles/ENV |awk -F= '{print $2}'
	echo "
	CREATE DATABASE $s_name;
	GRANT ALL PRIVILEGES ON $s_name.* TO '$s_name'@'localhost' IDENTIFIED BY '$PASS';
	GRANT ALL PRIVILEGES ON $s_name.* TO '$s_name'@'%' IDENTIFIED BY '$PASS';
	"|mysql --password=$Database_password

done

## END Create DBs
echo END Create DBs

##Configure files openstack
echo Configure files openstack

tmpfile=`mktemp`;
for fname in `ls $SCRIPT_PATH/conffiles/ |grep _etc`; do
	conffile=`echo $fname | sed s#_#/#g`;
	cat $SCRIPT_PATH/conffiles/$fname | while read line; do
		echo "export atr=\"$line\"" > $tmpfile;
		source $tmpfile
		rm -f $tmpfile
		openstack-config --set $conffile $atr
	done
done

## END Configure files openstack
echo END Configure files openstack

## Keystone config and start
echo Keystone config and start

keystone-manage pki_setup --keystone-user keystone --keystone-group keystone

chown -R keystone:keystone /etc/keystone/ssl
chmod -R o-rwx /etc/keystone/ssl

(crontab -l -u keystone 2>&1 | grep -q token_flush) || \
echo '@hourly /usr/bin/keystone-manage token_flush >/var/log/keystone/keystone-tokenflush.log 2>&1' >> /var/spool/cron/keystone

service openstack-keystone start
chkconfig openstack-keystone on

su -s /bin/sh -c "keystone-manage db_sync" keystone

## End Keystone config and start
echo End Keystone config and start

## Keystoune setting:
echo Keystoune setting:

### Create authfile
cat > ~/keystone.auth << EOF
export OS_USERNAME=admin
export OS_PASSWORD=$ADMIN_PASS
export OS_TENANT_NAME=admin
export OS_AUTH_URL=http://$controller:35357/v2.0
EOF

### Keystone:
keystone user-create --name=admin --pass=$ADMIN_PASS --email=$ADMIN_EMAIL
keystone role-create --name=admin
keystone tenant-create --name=admin --description="Admin Tenant"
keystone user-role-add --user=admin --tenant=admin --role=admin
keystone user-role-add --user=admin --role=_member_ --tenant=admin
keystone user-create --name=demo --pass=$DEMO_PASS --email=$DEMO_EMAIL
keystone tenant-create --name=demo --description="Demo Tenant"
keystone user-role-add --user=demo --role=_member_ --tenant=demo
keystone tenant-create --name=service --description="Service Tenant"
keystone service-create --name=keystone --type=identity --description="OpenStack Identity"
keystone endpoint-create   --service-id=$(keystone service-list | awk '/ identity / {print $2}') --publicurl=http://$controller:5000/v2.0 --internalurl=http://$controller:5000/v2.0 --adminurl=http://$controller:35357/v2.0


### Glance
keystone user-create --name=glance --pass=$GLANCE_PASS --email=$GLANCE_MAIL
keystone user-role-add --user=glance --tenant=service --role=admin
keystone service-create --name=glance --type=image --description="OpenStack Image Service"
keystone endpoint-create --service-id=$(keystone service-list | awk '/ image / {print $2}') --publicurl=http://$controller:9292  --internalurl=http://$controller:9292 --adminurl=http://$controller:9292

### Nova
keystone user-create --name=nova --pass=$NOVA_PASS --email=$NOVA_MAIL
keystone user-role-add --user=nova --tenant=service --role=admin
keystone service-create --name=nova --type=compute  --description="OpenStack Compute"
keystone endpoint-create --service-id=$(keystone service-list | awk '/ compute / {print $2}')  --publicurl=http


### Neutron
keystone user-create --name neutron --pass $NEUTRON_PASS --email $NEUTRON_EMAIL
keystone user-role-add --user neutron --tenant service --role admin
keystone service-create --name neutron --type network --description "OpenStack Networking"
keystone endpoint-create --service-id $(keystone service-list | awk '/ network / {print $2}') --publicurl http://$controller:9696 --adminurl http://$controller:9696 --internalurl http://$controller:9696

### Cinder
keystone user-create --name=cinder --pass=$CINDER_PASS --email=$CINDER_EMAIL
keystone user-role-add --user=cinder --tenant=service --role=admin
keystone service-create --name=cinder --type=volume --description="OpenStack Block Storage"
keystone endpoint-create  --service-id=$(keystone service-list | awk '/ volume / {print $2}')  --publicurl=http://$controller:8776/v1/%\(tenant_id\)s  --internalurl=http://$controller:8776/v1/%\(tenant_id\)s  --adminurl=http://$controller:8776/v1/%\(tenant_id\)s
keystone service-create --name=cinderv2 --type=volumev2 --description="OpenStack Block Storage v2"
keystone endpoint-create  --service-id=$(keystone service-list | awk '/ volumev2 / {print $2}') --publicurl=http://$controller:8776/v2/%\(tenant_id\)s  --internalurl=http://$controller:8776/v2/%\(tenant_id\)s  --adminurl=http://$controller:8776/v2/%\(tenant_id\)s

## End Keystoune setting
echo End Keystoune setting

## Last seting
echo Last seting

cd  /etc/neutron/
ln -s /etc/neutron/plugins/ml2/ml2_conf.ini /etc/neutron/plugin.ini
cp /etc/init.d/neutron-openvswitch-agent /etc/init.d/neutron-openvswitch-agent.orig
sed -i 's,plugins/openvswitch/ovs_neutron_plugin.ini,plugin.ini,g' /etc/init.d/neutron-openvswitch-agent
echo "include /etc/cinder/volumes/*" >> /etc/tgt/targets.conf


## End last setting
echo  End last setting


## Dashboard setting
echo Dashboard setting

cat > ~/local_settings.patch << EOF
15c15
< ALLOWED_HOSTS = ['horizon.example.com', 'localhost']
---
> ALLOWED_HOSTS = ['alice22', 'localhost']
107c107,108
<         'BACKEND' : 'django.core.cache.backends.locmem.LocMemCache'
---
>         'BACKEND' : 'django.core.cache.backends.locmem.LocMemCache',
>       'LOCATION' : '127.0.0.1:11211'
EOF

patch -f /etc/openstack-dashboard/local_settings  ~/local_settings.patch         

## End Dashboard setting
echo End Dashboard setting


## Service start and check
echo Service start and check

for serv in $service02 $service03; do
        service $serv restart;
        chkconfig $serv on;
done

## END Service start and check
echo END Service start and check

## DB sync
echo DB sync
su -s /bin/sh -c "cinder-manage db sync" cinder
su -s /bin/sh -c "glance-manage db_sync" glance
su -s /bin/sh -c "nova-manage db sync" nova

## End sync
echo End sync

	







